/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.comm.tcp;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketTimeoutException;

import blueprint4j.db.DBTools;
import blueprint4j.utils.ThreadScheduable;
import blueprint4j.utils.ThreadSchedule;


public class ProcessFactory implements ThreadScheduable {

	private ServerSocket serversocket = null;
	private Processable process = null;

	public ProcessFactory(Processable process) throws IOException {
		this.process = process;
		serversocket = new ServerSocket(process.getListenPort());
		serversocket.setSoTimeout(2000);
	}

	public boolean keepAlive() {return true;}

	public int sleepTime() {return 0;}

	public void process()
	throws Throwable {
		try {
			ThreadSchedule.add(process.getNewInstance(DBTools.getNDC(),serversocket.accept()));
		} catch (SocketTimeoutException ste) {}
	}

	public void close()
	throws Throwable {
		serversocket.close();
	}
}
